<?php
namespace Admin\Controller;

use Think\Page;
use Common\Base\ControllerBase;
use Common\Logic\Admin\AdminLogic;
use Common\Logic\Admin\GroupLogic;

class AdminController extends ControllerBase
{
	public $L_admin;

	public function _initialize()
	{
		$this->L_admin = new AdminLogic();
	}

	//用户列表
    public function admin_list()
    {
    	$L_group = new GroupLogic();
        $param = I('get.');
        $page = intval(I('get.p')); if($page < 1){ $page = 1; }
        $pageSize = 10;
        $param['root'] = false;
        $list = $this->L_admin->getAdminList(3, $param, 'a.admin_id,a.admin_group_id,a.admin_name,a.st,b.group_name', $page, $pageSize);
        $pg = new Page($list['tot'], $pageSize);
        $this->assign('groups', $L_group->getGroupSelect($param['admin_group_id'], 'admin_group_id'));
        $this->assign('list', $list['datas']);
        $this->assign('pager', $pg->show());
        $this->assign('param', $param);
        $this->display();
    }

    //ajax更改用户状态
    public function ajax_admin_st()
    {
        $st = (int)I('request.st', 0); if($st!=0 && $st!=1){ $st = 0; }
        $rs = $this->L_admin->updateAdminByAdminId(array('st'=>$st), I('request.admin_id', 0));
        if(false === $rs){
            $this->ajaxReturn(genReturn('n', $this->L_logic->getError()));
        }else{
            $this->ajaxReturn(genReturn('y', 'ok'));
        }
    }

    //添加用户
    public function admin_add()
    {
        if(IS_POST){
            $post = I('post.');
            $rs = $this->L_admin->addAdmin($post);
            if(false === $rs){
                $this->error($this->L_admin->getError());
            }else{
                $this->success('操作成功！', U('admin/admin_list'));
            }
            exit;
        }
        $L_group = new GroupLogic();
        $this->assign('groups', $L_group->getGroupSelect(-1, 'admin_group_id', 'getGroupAcl(this.value)'));
        $this->display();
    }

    //得到节点checkbox
    public function ajax_get_nodes_checkbox()
    {
        $L_group = new GroupLogic();
        $group_id = abs((int)I('get.group_id', 0));
        $this->ajaxReturn(genReturn('y', 'ok', array('htm'=>$L_group->getNodesToCheckbox($group_id))));
    }

    //修改用户
    public function admin_edit()
    {
        if(IS_POST){
            $post = I('post.');
            $rs = $this->L_admin->editAdmin($post);
            if(false === $rs){
                $this->error($this->L_admin->getError());
            }else{
                $this->success('操作成功！', U('admin/admin_list'));
            }
            exit;
        }
        $L_group = new GroupLogic();
        $admin_id = (int)I('get.admin_id', 0);
        if($admin_id < 2){ $this->error('参数错误！'); } //不能修改root
        $detail = $this->L_admin->getAdminList(4, array('admin_id'=>$admin_id));
        if(empty($detail)){ $this->error('没有找到此用户'); }
        $this->assign('detail', $detail);
        $this->assign('groups', $L_group->getGroupSelect($detail['admin_group_id'], 'admin_group_id', 'getGroupAcl(this.value)', true));
        $this->assign('nodes', $L_group->getNodesToCheckbox($detail['admin_group_id'], $detail['admin_id']));
        $this->display();
    }

}